
//
// 101115 - AH  - Created.
// 101124 - LLC - Made this rethor talk nicer.
// 101214 - AH  - (Hector) Now talk method only handles response from Forest
//                for testing: todo: Redesign so handles all.
// 101219 - AH  - Did todo of 101214, changes here relates to 
//                RethorAnswer.{101219b...c}.
// 110119 - AH - Implemented networkTalk(List<AttributeObject>) (was nop).
// 110327 - AH - Fixes related to get SpareWheel work in chat.
// 110702 - AH - (Garage) Deprecated.
//

package org.soekrat.engine.rethor;

import static org.soekrat.engine.rethor.RethorUtil.centrifuge;
import static org.soekrat.engine.rethor.RethorUtil.retrievedTreesToString;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.soekrat.AttributeObject;
import org.soekrat.engine.mine.Resource;
import org.soekrat.engine.mine.Tags;
import org.soekrat.engine.network.Cluster;
import org.soekrat.engine.network.NetworkNode;

/**
 * <p>
 *   A dummy 
 *   {@link Rethor} implementation for early testing and prototyping.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@Deprecated
public final class DummyRethor implements Rethor 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DummyRethor(){}
  
  // Specified in Rethor.
  public RethorAnswer talk(List<AttributeObject> paint, 
    List<AttributeObject> swpaint)
  {
	  if (!swpaint.isEmpty())
	  {
	    // If the spare wheel painted something it means the Wheel did not.
		
	    // Spare wheel output is always from a Forest.
	    RethorAnswer retVal=swForestTalk(swpaint);
	    return retVal;
	  }
	  else
	  {
	    // Ok, the spare wheel did not produce any, means probably the wheel did.
		
      List<AttributeObject> resources=new ArrayList<AttributeObject>();
      List<AttributeObject> trees=new ArrayList<AttributeObject>();
      List<AttributeObject> clusters=new ArrayList<AttributeObject>();
      List<AttributeObject> products=new ArrayList<AttributeObject>();
    
      int n=centrifuge(paint, resources, trees, clusters, products);
      switch (n)
      {
        case 0 :
          return new RethorAnswer("it's getting boring", paint);
      
        case 1 :
               if (!resources.isEmpty()) return mineTalk(resources);
          else if (!trees.isEmpty())     return forestTalk(trees);
          else if (!clusters.isEmpty())  return networkTalk(clusters);
          else if (!products.isEmpty())  return ProductTableTalk(clusters);
          else                           throw new Error();
      
        default :
          return mixedTalk(paint, resources, trees, clusters);
      }
    }
  }
  
  private RethorAnswer mixedTalk(List<AttributeObject> paint, 
    List<AttributeObject> resources, List<AttributeObject> trees, 
    List<AttributeObject> clusters)
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    if (!resources.isEmpty())
    {
      pw.println("Mine:");
      RethorAnswer ra=mineTalk(resources);
      pw.println(ra.getAnswer());
    }
    if (!trees.isEmpty())
    {
      pw.println("Forest:");
      RethorAnswer ra=forestTalk(trees);
      pw.println(ra.getAnswer());
    }
    if (!clusters.isEmpty())
    {
      pw.println("Network:");
      RethorAnswer ra=networkTalk(clusters);
      pw.println(ra.getAnswer());
    }
    
    pw.flush();
    
    return new RethorAnswer(caw.toString(), paint).setResources(resources)
      .setTrees(trees).setClusters(clusters);
  }
  
  private RethorAnswer forestTalk(List<AttributeObject> paint)
  {
    int n=(paint==null) ? 0 : paint.size();
    
    switch (n)
    {
      case 0 : return new RethorAnswer("it's getting boring", paint);
      
      case 1 :
      {
        //System.out.println(
        //  "Seed should grow, there is hope for a forest one day...");
        String str="Seed should grow, there is hope for a forest one day...";
        return new RethorAnswer(str, paint).setTrees(paint);
      }
      
      default :
      {
        String str=retrievedTreesToString(paint);
        return new RethorAnswer(str, paint).setTrees(paint);
      }
    }
    
  }
  
  private RethorAnswer swForestTalk(List<AttributeObject> paint)
  {
    StringBuilder sb=new StringBuilder();
    
    boolean first=true;
    for (AttributeObject curr : paint) 
    {
	    if (!first) sb.append(" ");
	    first=false;
	    sb.append(curr.getName());
	  }
    
    return new RethorAnswer(sb.toString(), paint);
  }
  
  private RethorAnswer mineTalk(List<AttributeObject> paint)
  {
    int n=(paint==null) ? 0 : paint.size();
    
    switch (n)
    {
      case 0 : return new RethorAnswer("it's getting boring", paint);
      
      case 1 :
      { 
        String str="Resource name is '"+getResourceToString(paint.get(0));
        return new RethorAnswer(str, paint).setResources(paint);
      }
      
      default :
      {
        StringBuilder ressb=new StringBuilder("Resources names are ");
        
        boolean first=true;
        for (AttributeObject curr : paint) 
        {
          if (first) ressb.append(", ");
          ressb.append("'").append(getResourceToString(curr)).append("'");
        }
          
        return new RethorAnswer(ressb.toString(), paint).setResources(paint);
      }    
    }
    
  }
  
  private RethorAnswer ProductTableTalk(List<AttributeObject> paint)
  {
    return new RethorAnswer(
      "If you see this you are smoking stuff man, way out, wow!", paint);
  }
  
  private final String getResourceToString(AttributeObject resource)
  {
    StringBuilder sb=new StringBuilder();
    
    sb.append(resource.getName()).append(" (");
    Tags tags=(Tags)resource.getAttribute("tags");
    sb.append(tags);
    sb.append(")");
    
    return sb.toString();
  }
  
  private RethorAnswer networkTalk(List<AttributeObject> paint)
  {
    StringBuilder sb=new StringBuilder();
    
    boolean first=true;
    for (AttributeObject curr : paint)
    {
      if (curr instanceof Cluster)
      {
        Cluster cluster=(Cluster)curr;
        for (NetworkNode node : cluster)
        {
          if (!first) sb.append(", ");
          first=false;
          sb.append(node.getName());
        }
      }
    }
    
    // TODO : Dummy implementation, real awaits the implementation of Network
    //        retrieval strategy.
    return new RethorAnswer(sb.toString(), paint).setClusters(paint);
  }

  // Specified in Rethor.
  public String visitorFeedback(VisitorAction va, AttributeObject paint) 
  {
    if (paint instanceof Resource)
    {
      switch (va)
      {
        case OK   : return "Anything to tag?";
        case HMMM : return "Next";
        default : throw new IllegalArgumentException("Uknown user action: "+va);
      }
    }
    else
    {
      return "";
    }
  }
  
}
